﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Gift.DAL;
using Gift.Model;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;
using AjaxPro;

namespace WebApp.admin.order
{
    public partial class order : BaseAdmin
    {
        // Fields

        protected tbOrder oOrder = new tbOrder();
        protected string ot = "";
        protected string registerIP = "";
        protected string registerTime = "";



        // Methods
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
            try
            {
                if (base.Request.QueryString["id"] != null)
                {
                    this.oOrder = BaseFactory.theOrder.GetModel(Convert.ToInt32(base.Request.QueryString["id"]));
                    this.oOrder.goodsTotal = Convert.ToDecimal(this.lblGoodsTotal.InnerText.Replace("￥", "").Replace("元", ""));
                    this.oOrder.orderTotal = Convert.ToDecimal(this.lblGoodsTotal.InnerText.Replace("￥", "").Replace("元", "")) + Convert.ToDecimal(this.txtDeliveryMoney.Value);
                    this.oOrder.deliveryWay = base.Request.Form["deliveryWay"];
                    this.oOrder.payment = this.selectPayment.Value;
                    this.oOrder.deliveryMoney = Convert.ToDecimal(this.txtDeliveryMoney.Value);
                    this.oOrder.postscript = this.spanPostscript.InnerHtml;
                    this.oOrder.payStatus = Convert.ToInt32(this.selectPayStatus.Value);
                    this.oOrder.deliveryStatus = Convert.ToInt32(this.selectDeliveryStatus.Value);
                    this.oOrder.orderStatus = Convert.ToInt32(this.selectOrderStatus.Value);
                    this.oOrder.consignee = this.txtConsignee.Value;

                    tbRegion oRegion = BaseFactory.theRegion.GetModel(Convert.ToInt32(base.Request.Form["provinces"]));
                    this.oOrder.region = oRegion.regionName;
                    if (base.Request.Form["city"] != null)
                    {
                        this.oOrder.region = this.oOrder.region + "," + base.Request.Form["city"];
                    }
                    this.oOrder.address = this.txtAddress.Value;
                    this.oOrder.zipcode = this.txtZipCode.Value;
                    this.oOrder.telephone = this.txtTelephone.Value;
                    this.oOrder.mobile = this.txtMobile.Value;
                    this.oOrder.presentedIntegra = Convert.ToInt32(this.txtPresentedIntegra.Value);
                    if (BaseFactory.theOrder.Update(this.oOrder))
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"orderList.aspx\";</script>");
                        base.Alert("");
                        
                    }
                    else
                    {
                        base.Alert("alert(\"更新失败！\");");
                       
                    }
                }
            }
            catch
            {
                base.Alert("alert(\"更新失败！\");");
               
            }
        }

        protected StringBuilder DeliveryWayBind()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                DataTable deliveryWayByCompanyId = BaseFactory.theDeliveryWay.GetDeliveryWayByCompanyId(int.Parse(base.Administrator.CompanyId));
                oOrder = BaseFactory.theOrder.GetModel(Convert.ToInt32(base.Request.QueryString["id"]));
                if (oOrder != null)
                {
                    foreach (DataRow row in deliveryWayByCompanyId.Rows)
                    {
                        builder.AppendLine("<option value=\"" + row["deliveryWayName"].ToString() + "\" " + ((row["deliveryWayName"].ToString() == oOrder.deliveryWay) ? "selected='selected'" : "") + ">" + row["deliveryWayName"].ToString() + "</option>");
                    }

                    builder.AppendLine("<option value=\"卖家承担运费\" " + (("卖家承担运费" == oOrder.deliveryWay) ? "selected='selected'" : "") + ">卖家承担运费</option>");
                }
                return builder;
            }
            catch
            {
                return builder;
            }
        }

        protected string GetGoodsEntitysSpecifications(object goodsEntitysId)
        {
            string str = "";
            try
            {
                DataTable table = BaseFactory.theGoodsEntitysSpecifications.FrontDeskGetGoodsEntitysSpecificationsByGoodsEntitys(Convert.ToInt32(goodsEntitysId));
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    object obj2 = str;
                    str = string.Concat(new object[] { obj2, (i > 0) ? "&nbsp;&nbsp;" : "", "[", table.Rows[i]["specificationsName"], "：", table.Rows[i]["specificationsValueName"], "]" });
                }
            }
            catch
            {
            }
            return str;
        }

        private void OrderBind()
        {
            try
            {
                oOrder = BaseFactory.theOrder.GetModel(Convert.ToInt32(base.Request.QueryString["id"]));
                if (oOrder != null)
                {
                    this.lblOrderNo.InnerText = oOrder.orderNo;
                    this.lblCreateTime.InnerText = oOrder.createTime.ToString("yyyy-MM-dd");
                    this.lblGoodsTotal.InnerText = "￥" + oOrder.goodsTotal.ToString() + "元";
                    this.lblOrderTotal.InnerText = "￥" + oOrder.goodsTotal.ToString() + "元";
                    this.selectPayment.Value = oOrder.payment;
                    this.txtDeliveryMoney.Value = oOrder.deliveryMoney.ToString();
                    this.selectPayStatus.Value = oOrder.payStatus.ToString();
                    this.spanPostscript.InnerHtml = oOrder.postscript;
                    this.spOrderRemark.InnerHtml = oOrder.orderRemark;
                    this.selectDeliveryStatus.Value = oOrder.deliveryStatus.ToString();
                    this.selectOrderStatus.Value = oOrder.orderStatus.ToString();
                    this.txtConsignee.Value = oOrder.consignee;
                    this.txtAddress.Value = oOrder.address;
                    this.txtZipCode.Value = oOrder.zipcode;
                    this.txtTelephone.Value = oOrder.telephone;
                    this.txtMobile.Value = oOrder.mobile;
                    this.txtPresentedIntegra.Value = oOrder.presentedIntegra.ToString();
                }
            }
            catch
            {
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Utility.RegisterTypeForAjax(typeof(Region_ajax));
            if (!base.IsPostBack && ((base.Request.QueryString["id"] != null) && (base.Request.QueryString["orderNo"] != null)))
            {
                this.OrderBind();
                this.rptOrderGoodsEntitysBind();
            }
        }

        protected StringBuilder PrintBind()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                if ((base.Request.QueryString["id"] != null) && (base.Request.QueryString["orderNo"] != null))
                {
                    builder.AppendLine("<input id=\"btnPrint\" type=\"button\" class=\"formButton\" value=\"打印订单\" onclick=\"javascript:window.open('orderReportForms.aspx?id=" + base.Request.QueryString["id"] + "&orderNo=" + base.Request.QueryString["orderNo"] + "');\" />");
                }
            }
            catch
            {
            }
            return builder;
        }

        protected StringBuilder ProvincesBind()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                builder.AppendLine("<select id=\"selectProvinces\" name=\"provinces\">");
                builder.AppendLine("<option value=\"0\">请选择...</option>");
                DataTable regionByParentRegion = BaseFactory.theRegion.GetRegionByParentRegionAndCompanyId(0, int.Parse(base.Administrator.CompanyId));
                oOrder = BaseFactory.theOrder.GetModel(Convert.ToInt32(base.Request.QueryString["id"]));
                string str = (oOrder.region.IndexOf(",") == -1) ? oOrder.region.ToString() : oOrder.region.Split(new char[] { ',' }).GetValue(0).ToString();
                string str2 = (oOrder.region.IndexOf(",") == -1) ? oOrder.region : oOrder.region.Split(new char[] { ',' }).GetValue(1).ToString();
                int parentRegion = 0;
                foreach (DataRow row in regionByParentRegion.Rows)
                {
                    if (str == row["regionName"].ToString())
                    {
                        parentRegion = Convert.ToInt32(row["id"]);
                    }
                    builder.AppendLine(string.Concat(new object[] { "<option value=\"", row["id"], "\" ", (str == row["regionName"].ToString()) ? "selected='selected'" : "", ">", row["regionName"], "</option>" }));
                }
                builder.AppendLine("</select><span id=\"spanCity\" style=\"color: #000000;\">");
                DataTable table3 = BaseFactory.theRegion.GetRegionByParentRegionAndCompanyId(parentRegion, int.Parse(base.Administrator.CompanyId));
                if (table3.Rows.Count > 0)
                {
                    builder.AppendLine("<select id=\"selectCity\" name=\"city\">");
                    builder.AppendLine("<option value=\"0\">请选择...</option>");
                    foreach (DataRow row in table3.Rows)
                    {
                        builder.AppendLine(string.Concat(new object[] { "<option value=\"", row["regionName"], "\" ", (str2 == row["regionName"].ToString()) ? "selected='selected'" : "", ">", row["regionName"], "</option>" }));
                    }
                    builder.AppendLine("</select>");
                }
                builder.AppendLine("</span>");
            }
            catch
            {
            }
            return builder;
        }

        protected void rptOrderGoodsEntitys_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
        }

        private void rptOrderGoodsEntitysBind()
        {
            this.rptOrderGoodsEntitys.DataSource = BaseFactory.theOrderGoodsEntitys.GetOrderGoodsEntitysByOrderNo(base.Request.QueryString["orderNo"]);
            this.rptOrderGoodsEntitys.DataBind();
        }
    }
}